//
//  FFBankcardListVM.m
//  LLWallet
//
//  Created by tt on 2020/9/9.
//

#import "FFBankcardListVM.h"
#import "FFBankcardListCell.h"
#import "FFBankcardListAddCell.h"
#import "FFBankcardAddVC.h"
@interface FFBankcardListVM ()

@property(nonatomic,strong) NSArray<FFBankcardListResp*> *items;

@end

@implementation FFBankcardListVM


- (NSArray<NSDictionary *> *)tableSectionMaps {
    __weak typeof(self) weakSelf = self;
    NSMutableArray<NSDictionary*> *itemDicts = [NSMutableArray array];
    if(self.items) {
        for (FFBankcardListResp *item in self.items) {
            [itemDicts addObject:@{
                @"height":@(10.0f),
                @"items":@[
                        @{
                            @"class": FFBankcardListModel.class,
                            @"bankName": item.bankname?:@"",
                            @"bankLogo":item.bankLogo?:@"",
                            @"bankBg": item.bankCover?:@"",
                            @"bankType": item.bankcardTypeName?:@"",
                            @"bankcard": item.bankcard?:@"",
                            @"onClick":^{
                                if(weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(ff_bankcardList:click:)]) {
                                    [weakSelf.delegate ff_bankcardList:weakSelf click:item];
                                }
                            }
                        }
                ],
            }];
        }
    }
   
    [itemDicts addObject:@{
         @"height":@(10.0f),
         @"items":@[
                 @{
                     @"class": FFBankcardListAddModel.class,
                     @"onClick":^{
                        [[WKNavigationManager shared] pushViewController:[FFBankcardAddVC new] animated:YES];
                     }
                 }
         ],
        
    }];
    return itemDicts;
}

- (void)requestData:(void (^)(NSError * _Nullable))complete {
    __weak typeof(self) weakSelf = self;
    [[WKAPIClient sharedClient] GET:@"impay/bankcards" parameters:nil model:FFBankcardListResp.class].then(^(NSArray<FFBankcardListResp*> *resps){
        weakSelf.items = resps;
        complete(nil);
    }).catch(^(NSError *error){
        complete(error);
    });
}

@end

@implementation FFBankcardListResp

+ (WKModel *)fromMap:(NSDictionary *)dictory type:(ModelMapType)type {
    FFBankcardListResp *resp = [FFBankcardListResp new];
    resp._id = [dictory[@"id"] integerValue];
    resp.bankcardType = dictory[@"bankcard_type"];
    resp.bankcardTypeName = dictory[@"bankcard_type_name"];
    resp.bankcard = dictory[@"bankcard"];
    resp.bankname = dictory[@"bankname"];
    resp.bankcode = dictory[@"bankcode"];
    resp.bankLogo = dictory[@"bank_logo"];
    resp.bankCover = dictory[@"bank_cover"];
    return resp;
}


@end
